<template>
  <div class="register-container">
    <div class="logo">
      <img src="@/assets/images/登录页logo.png"
           alt="">
    </div>
    <div class="register">
      <el-form ref="regFrom"
               :model="regFrom"
               :rules="regRules"
               class="reg-from">
        <div class="title">
          <h3>后台管理系统</h3>
        </div>
        <div class="link">
          <router-link to="/login">去登录</router-link>
        </div>
        <el-form-item prop="username">
          用户名
          <el-input type="text"
                    placeholder="请输入用户名"
                    v-model="regFrom.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          密码
          <el-input placeholder="请输入密码"
                    type="password"
                    v-model="regFrom.password"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          密码
          <el-input placeholder="请再次输入密码"
                    type="password"
                    v-model="regFrom.repassword"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click.native.prevent="submit">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: 'RegisterPage',
  data () {
    const validaterepass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.regFrom.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      regFrom: {
        username: 'zzzxxx',
        password: 'aaaaaa',
        repassword: 'aaaaaa'
      },
      regRules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }, {
          min: '1',
          max: '10',
          trigger: 'blur',
          message: '密码位数请保持在1-10位之间！'
        }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }, {
          min: '6',
          max: '15',
          trigger: 'blur',
          message: '密码位数请保持在6-15位之间！'
        }],
        repassword: [{ validator: validaterepass, trigger: 'blur' }]
      },
      loading: false
    }
  },
  methods: {
    submit () {
      this.$refs.regFrom.validate(async (isOK) => {
        if (isOK) {
          try {
            this.loading = true
            await register(this.regFrom)
            this.$router.push('/login')
          } catch (err) {
            console.log(err)
          } finally {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  padding: 0;
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  background-image: url('~@/assets/images/登录注册bg.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  position: fixed;

  .logo {
    img {
      width: 110px;
      height: 48px;
      position: relative;
      left: 50px;
      top: 30px;
    }
  }

  .register {
    width: 540px;
    height: 560px;
    position: absolute;
    box-sizing: border-box;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
    padding: 48px 60px 60px;

    .title {
      h3 {
        font-size: 32px;
        color: #131523;
        text-align: center;
        font-weight: normal;
        line-height: 44px;
        // background-color: pink;
        margin: 0;
      }
    }

    .link {
      text-align: center;
      padding: 8px 0 40px;

      a {
        text-decoration: none;
        color: #1e5eff;
        font-size: 16px;
      }
    }

    .el-form-item {
      color: #5a607f;
    }

    .el-button {
      border-radius: 8px;
      width: 100%;
      height: 48px;
      background-color: #4f81ff;
    }
  }
}
</style>
